import os

# 文件复制
"""
 原文件：F:\test.jpg
 目标文件：F:\test.jpg

 with: 结合open使用，自动释放资源
 open：只能是文件
 
 os.path : 对系统中文件的操作
    os.path.dirname 获取文件夹
    os.path.join 拼接路径

"""
print("复制文件".center(30, "*"))
with open(r"F:\test.jpg", 'rb') as r_stream:
    content = r_stream.read()
    abs_file_name = r_stream.name
    print(abs_file_name)
    start_index = abs_file_name.rfind("\\") + 1
    print(abs_file_name[start_index:])  # 截取文件名
    with open(r"F:\test1.jpg", 'wb') as w_stream:
        w_stream.write(content)

print("文件复制完成！")

print("批量复制文件".center(30, "*"))

print(os.path)
path = os.path.dirname(__file__)
print(path)  # 当前文件的路径
print(type(path))

new_path = os.path.join(path, "test.jpg")
print(new_path)

print("复制文件到当前文件夹下".center(30, "*"))
with open(r"F:\test.jpg", 'rb') as r_stream:
    content = r_stream.read()
    new_path = os.path.join(os.path.dirname(__file__), "test.jpg")
    with open(new_path, 'wb') as w_stream:
        w_stream.write(content)
print("end-------------完成文件复制")
